//
//  ChartTool.m
//  Daybook
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "ChartTool.h"
#import "CalendarTool.h"
#import "Common.h"

@interface ChartTool ()

@property (nonatomic, strong)RLMRealm *realm;

@property (nonatomic, strong)RLMResults *accountBooks;

@end
@implementation ChartTool

+ (ChartTool *)shareChartTool {
    static ChartTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[ChartTool alloc] init];
    });
    return tool;
}

- (BOOL)isNullInDataBaseWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate type:(NSString *)type {
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *lookResult = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, type];
    if (lookResult.count == 0) {
        return YES;
    }else {
        return NO;
    }
}


- (NSMutableDictionary *)classNameDictionaryWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    NSMutableDictionary *classNameDic = [NSMutableDictionary dictionary];
    NSMutableSet *nameSet = [NSMutableSet set];
    NSMutableSet *tempSet = [NSMutableSet set];
    NSMutableArray *incomeArr = [NSMutableArray array];
    NSMutableArray *outLayArr = [NSMutableArray array];
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *accountBooks = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"1"];

    for (Account *modal in accountBooks) {
        [nameSet addObject:modal.name];
    }
    for (NSString *nameStr in nameSet) {
        [incomeArr addObject:nameStr];
    }

    RLMResults *account = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"2"];

    for (Account *modal in account) {
        [tempSet addObject:modal.name];
    }
    for (NSString *nameStr in tempSet) {
        [outLayArr addObject:nameStr];
    }
    
    [classNameDic setValue:incomeArr forKey:@"incomeArr"];
    [classNameDic setValue:outLayArr forKey:@"outLayArr"];

    
    return classNameDic;
}

- (NSMutableArray *)incomeArrayWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    NSMutableArray *chartArr = [NSMutableArray array];
    double totalMoney = 0;
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *account = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"1"];
    for (NSInteger i = 0; i < account.count; i++) {
        Account *incomeBook = account[i];
        totalMoney += incomeBook.income.doubleValue;
    }
 
    NSDictionary *classDic = [self classNameDictionaryWithBeginDate:beginDate endDate:endDate];
    NSArray *classNameArr = [classDic valueForKey:@"incomeArr"];
    for (NSInteger i = 0; i < classNameArr.count; i++) {
        NSString *classNameStr = classNameArr[i];
        
        RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
        DayBook *currentDayBooks = [result firstObject];
        
        RLMResults *accountBooks = [currentDayBooks.bookContent objectsWhere:@"time >  %@ and time < %@ and name = %@ and type = %@", beginDate, endDate, classNameStr, @"1"];

        Account *model = accountBooks[0];
        if ([model.type isEqualToString:@"1"]) {
            NSNumber *totalIncome = [accountBooks sumOfProperty:@"income"];
            ChartModel *chartModel = [ChartModel new];
            chartModel.picName = model.picName;
            chartModel.name = model.name;
            chartModel.type = model.type;
            chartModel.amount = [NSString stringWithFormat:@"%.2f", totalIncome.doubleValue];
            chartModel.proportionNum = [NSString stringWithFormat:@"%.2f%%", totalIncome.doubleValue / totalMoney * 100];

            [chartArr addObject:chartModel];
        }
        
    }
    chartArr = [self sortDescendingOfArray:chartArr];
    return chartArr;
}

- (NSMutableArray *)outLayArrayWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    NSMutableArray *chartArr = [NSMutableArray array];
    double totalMoney = 0;
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *account = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"2"];
    for (NSInteger i = 0; i < account.count; i++) {
        Account *incomeBook = account[i];
        totalMoney += incomeBook.money.doubleValue;
    }
    
    NSDictionary *classDic = [self classNameDictionaryWithBeginDate:beginDate endDate:endDate];
    NSArray *classNameArr = [classDic valueForKey:@"outLayArr"];
    for (NSInteger i = 0; i < classNameArr.count; i++) {
        NSString *classNameStr = classNameArr[i];
        
        RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
        DayBook *currentDayBooks = [result firstObject];
        
        RLMResults *accountBooks = [currentDayBooks.bookContent objectsWhere:@"time >  %@ and time < %@ and name = %@ and type = %@", beginDate, endDate, classNameStr, @"2"];

        Account *model = accountBooks[0];
        if ([model.type isEqualToString:@"2"]) {
            NSNumber *totalOutLay = [accountBooks sumOfProperty:@"money"];
            ChartModel *chartModel = [ChartModel new];
            chartModel.picName = model.picName;
            chartModel.name = model.name;
            chartModel.type = model.type;
            chartModel.amount = [NSString stringWithFormat:@"%.2f", totalOutLay.doubleValue];
            chartModel.proportionNum = [NSString stringWithFormat:@"%.2f%%", totalOutLay.doubleValue / totalMoney * 100];
            [chartArr addObject:chartModel];
        }
        
    }
    chartArr = [self sortDescendingOfArray:chartArr];
    return chartArr;
}

- (NSMutableArray *)sortDescendingOfArray:(NSMutableArray *)oldArray {
    for (NSInteger i = 0; i < oldArray.count; i++) {
        for (NSInteger j = 0; j < oldArray.count - i - 1; j++) {
            ChartModel *beforeModel = oldArray[j];
            ChartModel *nextModel = oldArray[j + 1];
            if (beforeModel.amount.doubleValue < nextModel.amount.doubleValue) {
                ChartModel *tempModel = oldArray[j];
                [oldArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                [oldArray replaceObjectAtIndex:j + 1 withObject:tempModel];
            }
        }
    }
    return oldArray;
}

- (NSMutableArray *)incomeDictionaryArrayWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    NSMutableArray *tempArr = [NSMutableArray array];
    NSMutableArray *incomeArr = [NSMutableArray arrayWithArray:[self incomeArrayWithBeginDate:beginDate endDate:endDate]];
    for (ChartModel *model in incomeArr) {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        NSString *moneyStr = [NSString stringWithFormat:@"%.2f", model.amount.doubleValue];
        NSNumber *moneyNum = [NSNumber numberWithDouble:moneyStr.doubleValue];
        [tempDic setValue:moneyNum forKey:@"money"];
        [tempDic setValue:model.name forKey:@"name"];
        [tempArr addObject: tempDic];
    }
//    NSLog(@"incomearrr: %@", tempArr);
    return tempArr;
}

- (NSMutableArray *)outLayDictionaryArrayWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    NSMutableArray *tempArr = [NSMutableArray array];
    NSMutableArray *outLayArr = [NSMutableArray arrayWithArray:[self outLayArrayWithBeginDate:beginDate endDate:endDate]];
    for (ChartModel *model in outLayArr) {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        NSString *moneyStr = [NSString stringWithFormat:@"%.2f", model.amount.doubleValue];
        NSNumber *moneyNum = [NSNumber numberWithDouble:moneyStr.doubleValue];
        [tempDic setValue:moneyNum forKey:@"money"];
        [tempDic setValue:model.name forKey:@"name"];
        [tempArr addObject: tempDic];
    }
//    NSLog(@"payarrr: %@", tempArr);
    return tempArr;
}

+ (NSString *)totalIncomeWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    double totalMoney = 0;
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *account = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"1"];
    for (NSInteger i = 0; i < account.count; i++) {
        Account *incomeBook = account[i];
        totalMoney += incomeBook.income.doubleValue;
    }
    return [NSString stringWithFormat:@"%.2f", totalMoney];
}

+ (NSString *)totalOutLayWithBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate; {
    double totalMoney = 0;
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *account = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and type = %@", beginDate, endDate, @"2"];
    for (NSInteger i = 0; i < account.count; i++) {
        Account *incomeBook = account[i];
        totalMoney += incomeBook.money.doubleValue;
    }
    return [NSString stringWithFormat:@"%.2f", totalMoney];
}


@end
